package com.yueya.basic.notice.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.jfinal.aop.Before;
import com.jfinal.core.JFinal;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.basic.dept.model.Dept;
import com.yueya.basic.dept.model.DeptUser;
import com.yueya.basic.notice.model.Notice;
import com.yueya.basic.notice.model.NoticeDept;
import com.yueya.basic.notice.model.NoticeUser;
import com.yueya.basic.user.model.User;
import com.yueya.common.action.BaseController;
import com.yueya.common.model.Json;
import com.yueya.common.model.Tree;
import com.yueya.common.util.DateFormat;
import com.yueya.common.util.DateUtil;
import com.yueya.common.util.UUIDGenerator;

/**
 * 通知公告
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("unused")
public class NoticeController extends BaseController {

	@SuppressWarnings("rawtypes")
	Map map = new HashMap();

	public void index() {
		this.setAttr("flag", "0");
		render("/WEB-INF/views/basic/notice/list.jsp");
	}

	/**
	 * 完全展示 部门
	 */
	public void listTreeD() {
		String noticeId = getPara("noticeid");
		List<Tree> ret = Notice.service.getTreeNoticeDept(noticeId);
		renderJson(JsonKit.toJson(ret));
	}

	/**
	 * 完全展示 部门和 用户
	 */
	public void listTreeU() {

		String noticeId = getPara("noticeid");
		List<Tree> ret = Notice.service.getTreeNoticeUser(noticeId);

		renderJson(JsonKit.toJson(ret));
	}

	public void mylist() {// 我发布的
		this.setAttr("flag", "1");
		render("/WEB-INF/views/basic/notice/list.jsp");
	}

	@SuppressWarnings("unchecked")
	public void getListPager() {
		Record user = getUserWithCookie();
		String userid = user.getStr("user_id");

		map.clear();
		map.put("name", this.getPara("name"));
		map.put("flag", this.getPara("flag"));
		map.put("delflag", "0");
		map.put("userid", userid);
		Page<Record> page = Notice.service
				.pagination(getPage(), getRows(), map);
		jsonMap.put("total", page.getTotalRow());
		jsonMap.put("rows", page.getList());
		renderJson(JsonKit.toJson(jsonMap));
	}

	public void doAdd() {

		Record user = getUserWithCookie();
		String userid = user.getStr("user_id");
		String usernam = user.getStr("user_name");
		String deptid = DeptUser.dao.findByUserId(userid).getStr("dept_id");
		
		String deptnam = Dept.dao.findById(deptid).getStr("dept_name");
		this.setAttr("userid", userid);
		this.setAttr("usernam", usernam);
		this.setAttr("deptid", deptid);
		this.setAttr("deptnam", deptnam);

		this.render("/WEB-INF/views/basic/notice/edit.jsp");
	}

	public void doEdit() {
		String id = this.getPara("id");

		Record notice = Notice.service.getNoticeById(id);

		setAttr("notice", notice);

		this.render("/WEB-INF/views/basic/notice/edit.jsp");
	}

	public void save() throws Exception {

		Notice tzgg = getModel(Notice.class);
		String content = this.getPara("content");
		String scope = this.getPara("scope");
		String depts = this.getPara("depts_id");
		String users = this.getPara("users_id");

		boolean bl = Notice.service.save(tzgg, content, scope, depts, users);

		Json j = new Json();
		if (bl) {
			j.setSuccess(true);
			j.setMsg("提交成功");
		} else {
			j.setSuccess(false);
			j.setMsg("操作失败");
		}

		this.renderJson(j);
	}

	public void publish() {
		int num = Db.update(
				"update t_notice set status='1',pubdat=?  where id =?",
				DateUtil.getDate(DateUtil.timeFormat), this.getPara("id"));
		Json j = new Json();
		if (num > 0) {
			j.setSuccess(true);
			j.setMsg("发布成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("发布失败!");
		}
		this.renderJson(j);
	}

	public void recall() {

		int num = Db.update(
				"update t_notice set status='0',pubdat=? where id =?", "",
				this.getPara("id"));
		Json j = new Json();
		if (num > 0) {
			j.setSuccess(true);
			j.setMsg("撤回成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("撤回失败!");
		}
		this.renderJson(j);
	}

	public void remove() {
		int num = Db.update("update t_notice set delflag='1' where id =?",
				this.getPara("id"));
		Json j = new Json();
		if (num > 0) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

	public void bathremove() {
		Boolean bl = false;

		String idsStr = this.getPara("ids");

		if (idsStr != null) {
			String[] ids = idsStr.split(";");
			Notice tzgg = null;
			for (int i = 0; i < ids.length; i++) {

				if (!"".equals(ids[i]) && ids[i] != null) {
					tzgg = Notice.dao.findById(ids[i]);
					tzgg.set("delflag", 1);
					if (tzgg.update()) {
						bl = true;
					} else {
						bl = false;
						break;
					}
				}

			}
		}

		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

}
